﻿using BCCommon;
using BCDto.Common.Area;
using BCService.Common.Area;
using BCService.Utils;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;
using System.Net;

namespace BCTMSRESTful.Controllers.Management.Area
{
    /// <summary>
    /// 县信息接口
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class CountiesController : ManagementBaseController
    {
        private readonly ICountyService countyService;

        public CountiesController(ICountyService countyService)
        {
            this.countyService = countyService;
        }

        /// <summary>
        /// 获取一组县信息。
        /// </summary>
        /// <param name="provinceCode">省编号</param>
        /// <param name="cityCode">市编号</param>
        /// <param name="countyCode">县编号</param>
        /// <param name="index">自 1 开始的页码编号</param>
        /// <param name="size">每页显示的数据条数。</param>
        /// <response code="200">带有分页信息的县列表。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<CountyDto>), 200)]
        public IActionResult GetCounties([FromQuery(Name = "province_code")]string provinceCode,
            [FromQuery(Name = "city_code")]string cityCode, 
            [FromQuery(Name = "county_code")]string countyCode,
            [FromQuery(Name = "page_index")]int index = 1, 
            [FromQuery(Name = "page_size")]int size = 10)
        {
            return Ok(countyService.GetCounties(provinceCode, cityCode, countyCode, index, size));
        }

        /// <summary>
        /// 新增一个县信息
        /// </summary>
        /// <param name="dto">接收县必填信息</param>
        /// <response code="201">已创建的县信息。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("删除区县信息")]
        [ProducesResponseType(typeof(CountyDto), 201)]
        public IActionResult Add([FromBody]CountyRequestDto dto)
        {
            if (countyService.Add(dto,this.Admin))
            {
                var result = countyService.GetCountyByCode(dto.CountyCode);

                return StatusCode((int)HttpStatusCode.Created, result);
            }
            else {
                return BadRequest("添加区县信息失败");
            }
           
        }

        /// <summary>
        /// 获取指定的县信息
        /// </summary>
        /// <param name="countyCode"></param>
        /// <response code="200">获取的县信息。</response>
        /// <returns></returns>
        [HttpGet("{county_code}")]
        [ProducesResponseType(typeof(CountyDto), 200)]
        public IActionResult GetCountyById([FromRoute(Name ="county_code")]string countyCode)
        {
            return Ok(countyService.GetCountyByCode(countyCode));
        }

        /// <summary>
        /// 修改指定的县信息
        /// </summary>
        /// <param name="countyId">县Id</param>
        /// <param name="dto">要更新的县数据</param>
        /// <response code="200">修改后的县信息。</response>
        /// <returns></returns>
        [HttpPut("{county_id}")]
        [Log("修改指定区县信息")]
        [ProducesResponseType(typeof(CountyDto), 200)]
        public IActionResult Put([FromRoute(Name = "county_id")]long countyId, [FromBody]CountyRequestDto dto)
        {
            var putDto = dto.As<CountyPutDto>();
            putDto.CountyId = countyId;
            countyService.Update(putDto,this.Admin);
            return StatusCode((int)HttpStatusCode.OK, this.countyService.GetCountyById(countyId));
        }

        /// <summary>
        /// 删除指定的县信息
        /// </summary>
        /// <param name="countyCode">县编号</param>
        /// <returns></returns>
        /// <response code="204">删除成功后的提示信息。</response>
        [HttpDelete("{county_code}")]
        [Log("删除指定区县信息")]
        public IActionResult Delete([FromRoute(Name = "county_code")]string countyCode)
        {
            countyService.Delete(countyCode);
            return NoContent();
        }

        /// <summary>
        /// 修改区县需要议价
        /// </summary>
        /// <param name="countyCode">区县Id</param>
        /// <param name="isNegotiatedPrice">需要议价</param>
        /// <returns></returns>
        [HttpPut("[action]/{county_code}")]
        [Log("修改区县需要议价")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult NegotiatedPrice([FromRoute(Name = "county_code")]string countyCode, [FromQuery(Name = "is_negotiated_price")]bool isNegotiatedPrice)
        {
            return this.Ok(countyService.UpdateCountyNegotiatedPrice(countyCode, isNegotiatedPrice));
        }
    }
}
